﻿using System;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using vBridge.Dwm.Aero.Glassers;
using vBridge.Dwm.Aero.Margins;

namespace vBridge.Wpf.Aero
{
    /// <summary>
    ///     Wraps an aero-glasser and provides additional operations required to glass a WPF <see cref="Window" />.
    /// </summary>
    public class WpfWindowAeroGlasser : IWindowAeroGlasser
    {
        private readonly Window _window;
        private readonly IWindowAeroGlasser _windowAeroGlasser;

        /// <summary>
        ///     Initializes a new instance of the <see cref="WpfWindowAeroGlasser" /> class.
        /// </summary>
        /// <param name="windowAeroGlasser">Aero-glasser that will be used to aero-glass the window.</param>
        /// <param name="window">The window to aero-glass.</param>
        public WpfWindowAeroGlasser(IWindowAeroGlasser windowAeroGlasser, Window window)
        {
            _windowAeroGlasser = windowAeroGlasser;
            _window = window;
        }

        public void GlassWindow(IMargins margins)
        {
            _windowAeroGlasser.GlassWindow(margins);
            HwndSource hwndSource = HwndSource.FromHwnd(_windowAeroGlasser.WindowHandle);

            if (hwndSource == null) return;
            if (hwndSource.CompositionTarget == null) return;

            hwndSource.CompositionTarget.BackgroundColor = Colors.Transparent;
            _window.Background = Brushes.Transparent;
        }

        public IntPtr WindowHandle
        {
            get { return _windowAeroGlasser.WindowHandle; }
        }
    }
}